package com.duobaoyu.dby.supply.common.mq.remote.message.base;

import cn.hutool.core.util.IdUtil;
import com.duobaoyu.dby.common.core.constant.RequestConstants;
import com.duobaoyu.dby.common.core.util.SpringContextHolder;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.slf4j.MDC;

/**
 * 基础消息
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/28 9:12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(builderMethodName = "bBuilder")
public class BaseMessage {
	/**
	 * 发送消息来源，用于排查问题(不填默认当前项目名)
	 */
	private String source;
	/**
	 * 跟踪id(不填默认当前MDC日志链路,无链路自动生成)
	 */
	private String traceId;

	/**
	 * 默认数据填充
	 */
	public void defaultDataFill() {
		try {
			source = SpringContextHolder.getApplicationContext().getApplicationName();
		}catch (Exception e){
			source = "上下文未获取到项目名称";
		}

		//链路id填充
		String requestId = MDC.get(RequestConstants.REQUEST_ID);
		if (StringUtils.isNotBlank(requestId)) {
			traceId = requestId;
		} else {
			traceId = IdUtil.nanoId();
		}
	}
}
